package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.TCategory;
import com.ruoyi.system.domain.TProduct;
import com.ruoyi.system.service.TCategoryService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (TCategory)表控制层
 *
 * @author makejava
 * @since 2025-01-01 20:37:20
 */
@RestController
@RequestMapping("tCategory")
public class TCategoryController {
    /**
     * 服务对象
     */
    @Resource
    private TCategoryService tCategoryService;

    /**
     * 分页查询
     *
     * @param tCategory 筛选条件
     * @return 查询结果
     */
//    @GetMapping
//    public ResponseEntity<Page<TCategory>> queryByPage(
//            TCategory tCategory,
//            @RequestParam(defaultValue = "0") int page,
//            @RequestParam(defaultValue = "10") int size,
//            @RequestParam(required = false) String sortField,
//            @RequestParam(required = false, defaultValue = "asc") String sortOrder
//    ) {
//        Sort sort = (sortField != null && !sortField.isEmpty())
//                ? Sort.by(Sort.Direction.fromString(sortOrder), sortField)
//                : Sort.unsorted();
//
//        Pageable pageable = PageRequest.of(page, size, sort);
//        Page<TCategory> result = this.tCategoryService.queryByPage(tCategory);
//
//        return ResponseEntity.ok(result);
//    }

    @GetMapping("/level/{value}")
    public AjaxResult queryByLevel(@PathVariable("value") int value) {
        List<TProduct> productList = tCategoryService.queryByLevel(value);
        return AjaxResult.success(productList);
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<TCategory> queryById(@PathVariable("id") String id) {
        return ResponseEntity.ok(this.tCategoryService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param tCategory 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<TCategory> add(TCategory tCategory) {
        return ResponseEntity.ok(this.tCategoryService.insert(tCategory));
    }

    /**
     * 编辑数据
     *
     * @param tCategory 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<TCategory> edit(TCategory tCategory) {
        return ResponseEntity.ok(this.tCategoryService.update(tCategory));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(String id) {
        return ResponseEntity.ok(this.tCategoryService.deleteById(id));
    }

}

